#include<iostream>
#include<assert.h>
using namespace std;

template<class T>
struct AVLTreeNode
{
	AVLTreeNode(const T& data = T())
		: _left(nullptr)
		, _right(nullptr)
		, _parent(nullptr)
		, _data(data)
		, _bf(0)
	{}

	AVLTreeNode<T>* _left;
	AVLTreeNode<T>* _right;
	AVLTreeNode<T>* _parent;
	T _data;
	int _bf;
};

template<class T>
class AVLTree
{
	typedef AVLTreeNode<T> Node;
public:
	AVLTree()
		:_root(nullptr)
	{}

	//左单旋
	void RotateL(Node* root)
	{
		Node* Pparent = root->_parent;

		Node* rootR = root->_right;
		Node* rootRL = rootR->_left;

		root->_right = rootR->_left;
		if (rootRL)
			rootRL->_parent = root;

		rootR->_left = root;
		root->_parent = rootR;

		root->_bf = rootR->_bf = 0;
		//if(rootRL)
		//	rootRL->_bf = 0;

		if (root == _root)
		{
			_root = rootR;
			_root->_parent = nullptr;
		}
		else
		{
			if (Pparent->_left == root)
			{
				Pparent->_left = rootR;
				rootR->_parent = Pparent;
			}
			else if (Pparent->_right == root)
			{
				Pparent->_right = rootR;
				rootR->_parent = Pparent;
			}
			else
			{
				assert(false);
			}
		}
	}
	//右单旋
	void RotateR(Node* root)
	{
		Node* Pparent = root->_parent;

		Node* rootL = root->_left;
		Node* rootLR = rootL->_right;

		root->_left = rootL->_right;
		if (rootLR)
			rootLR->_parent = root;


		rootL->_right = root;
		root->_parent = rootL;

		root->_bf = rootL->_bf = 0;
		//if(rootLR)
		//	rootLR->_bf = 0;

		if (root == _root)
		{
			_root = rootL;
			_root->_parent = nullptr;
		}
		else
		{
			if (Pparent->_left == root)
			{
				Pparent->_left = rootL;
				rootL->_parent = Pparent;
			}
			else if (Pparent->_right == root)
			{
				Pparent->_right = rootL;
				rootL->_parent = Pparent;
			}
			else
			{
				assert(false);
			}
		}
	}
	//左右单旋
	void RotateLR(Node* root)
	{
		Node* rootL = root->_left;
		Node* rootLR = rootL->_right;
		int bf = rootLR->_bf;

		RotateL(rootL);
		RotateR(root);

		if (bf == -1)
		{
			root->_bf = 1;
			rootL->_bf = 0;
			if (rootLR)
				rootLR->_bf = 0;
		}
		else if (bf == 1)
		{
			rootL->_bf = -1;
			root->_bf = 0;
			if (rootLR)
				rootLR->_bf = 0;
		}
		else if (bf == 0)
		{
			rootL->_bf = root->_bf = rootLR->_bf = 0;
		}
		else
		{
			assert(false);
		}
	}
	//右左单旋
	void RotateRL(Node* root)
	{
		Node* rootR = root->_right;
		Node* rootRL = rootR->_left;

		int bf = rootRL->_bf;

		RotateR(rootR);
		RotateL(root);

		if (bf == -1)
		{
			root->_bf = -1;
			rootR->_bf = 0;
			if (rootRL)
				rootRL->_bf = 0;
		}
		else if (bf == 1)
		{
			rootR->_bf = 1;
			root->_bf = 0;
			if (rootRL)
				rootRL->_bf = 0;
		}
		else if (bf == 0)
		{
			rootR->_bf = root->_bf = rootRL->_bf = 0;
		}
		else
		{
			assert(false);
		}
	}

	bool Insert(const T& data)
	{
		//第一次插入
		if (_root == nullptr)
		{
			_root = new Node(data);
			return true;
		}
		Node* cur = _root;
		Node* parent = nullptr;
		while (cur)
		{
			if (data > cur->_data)
			{
				parent = cur;
				cur = cur->_right;
			}
			else if (data < cur->_data)
			{
				parent = cur;
				cur = cur->_left;
			}
			else
			{
				return false;
			}
		}
		cur = new Node(data);
		if (data > parent->_data)
		{
			parent->_right = cur;
			cur->_parent = parent;
		}
		else
		{
			parent->_left = cur;
			cur->_parent = parent;
		}
		//插入完成

		//控制平衡
		while (parent)
		{
			if (cur == parent->_left)
			{
				parent->_bf--;
			}
			else
			{
				parent->_bf++;
			}

			if (parent->_bf == 0)
			{
				return true;
			}
			else if (parent->_bf == 1 || parent->_bf == -1)
			{
				//继续往上更新
				cur = parent;
				parent = parent->_parent;
			}
			else if (parent->_bf == 2 || parent->_bf == -2)
			{
				//旋转处理

				//左旋
				if (cur->_bf == 1 && parent->_bf == 2)
				{
					RotateL(parent);
					break;
				}
				//右旋
				else if (cur->_bf == -1 && parent->_bf == -2)
				{
					RotateR(parent);
					break;
				}
				//左右双旋 ---> 最好也封装起来
				else if (parent->_bf == -2 && cur->_bf == 1)
				{
					RotateLR(parent);
					break;
				}
				//右左双旋
				else if (parent->_bf == 2 && cur->_bf == -1)
				{
					RotateRL(parent);
					break;
				}
				else
				{
					assert(false);
				}

			}
			else
			{
				assert(false);
			}
		}
		return true;
	}
	void InOrder()
	{
		_InOrder(_root);
	}

	void _InOrder(Node* root)
	{
		if (root == NULL)
			return;

		_InOrder(root->_left);
		cout << root->_data << " ";
		_InOrder(root->_right);
	}
	bool IsBalance()
	{
		return _IsBalance(_root);
	}

	int Height(Node* root)
	{
		if (root == NULL)
			return 0;
		int leftHeight = Height(root->_left);
		int rightHeight = Height(root->_right);
		return leftHeight > rightHeight ? leftHeight + 1 : rightHeight + 1;
	}

	bool _IsBalance(Node* root)
	{
		if (root == NULL)
			return true;

		// 对当前树进行检查
		int leftHeight = Height(root->_left);
		int rightHeight = Height(root->_right);

		if (rightHeight - leftHeight != root->_bf)
		{
			cout << root->_data << "现在是:" << root->_bf << endl;
			cout << root->_data << "应该是:" << rightHeight - leftHeight << endl;
			return false;
		}

		return abs(rightHeight - leftHeight) < 2
			&& _IsBalance(root->_left)
			&& _IsBalance(root->_right);
	}
private:
	Node* _root;
};